<?php
namespace app\admin\controller;
header("content-type:text/html;charset=utf-8");
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Apply;
use app\admin\model\Dustbin;
use app\admin\model\Teacher;
use app\admin\model\User;
use app\admin\model\Usertype;
use think\console\command\make\Controller;
use think\Db;
use think\Request;
use think\File;
class Teachers extends Base
{
  public $qr_src_path = "public/static/admin/uploads/teacher_qrcode/";

  //老师列表
  public function lists(){
    $teacher = collection(Teacher::all())->toArray();
    $th=['0'=>"自己"];
    foreach($teacher as $val){
      $th[$val['id']] = $val['name'];
    }
    $arr = [
     "setting"=>$this->getAllSetting(),
     "teacher"=>$teacher,
      "th"=>$th
    ];
    return view("lists",$arr);
  }

  //添加负责老师
  public function addform(){

    $arr = [
      "setting"=>$this->getAllSetting(),
      "teacher"=>collection(Teacher::select())->toArray()
    ];
    return view("addform",$arr);
  }
  //执行添加
  public function add(Request $request){
    //先去看看是否有这样的老师如有则不添加
    $data = $request->post();
    $res = Teacher::where(["name"=>$data['name'],"tel"=>$data['tel']])->find();
    if($res){
      $this->success("该老师以存在!");
    }else {
      $data['create_times'] = $data['update_times'] = time();
      $res = Teacher::create($data);
      $qr_code = $this->Qrcode(['tid'=>$res->getData()['id']]); //创建老师推广学生登录的二维码
      $ress = Teacher::update(['id'=>$res->getData()["id"],"qr_src"=>$qr_code]);
      if ($ress) {
        $this->success("添加成功", "Teachers/lists");
      } else {
        $this->error("添加失败");
      }
    }
  }
  //重新生成登录二维码
  public function re_qrcode(Request $request){
    $id = $request->post("id");
    //删除之前生成的二维码
    $res = Teacher::get($id);
    delete_file($this->qr_src_path.$res['qr_src']); //删除数据的时候连同生成的图片一起删除
    $qr_code = $this->Qrcode(['tid'=>$id]); //创建老师推广学生登录的二维码
    $ress = Teacher::update(['id'=>$id,"qr_src"=>$qr_code]);
    if ($ress) {
      $this->success($qr_code);
    } else {
      $this->error("更新失败");
    }
  }

  //生成二维码
  public function Qrcode($arr){
    $setting = $this->getAllSetting();
    //组织$query
    $query = "";
    foreach($arr as $key=>$val){
      $query .= $key."/".$val."/";
    }
    $query = substr($query,0,-1);
    $str = $setting['qrcode_url']."/".$query;
    return  $this->qr_code_dir($str,"teacher_qrcode");
  }

  public function test(){
    $this->Qrcode(['tid'=>1,"ser"=>45]);
  }

  //修改表单页面
  public function update(Request $request){
      $id = $request->get();
      $arr = [
        "setting" => $this->getAllSetting(),
        "data" => Teacher::get($id['id']),
        "teacher"=>collection(Teacher::select())->toArray()
      ];
      return view("update",$arr);
  }
  //执行修改
  public function toUpdate(Request $request){
    $data = $request->post();
    $data["update_times"]= time();
    if(Teacher::update($data)){
      $this->success("修改成功","Teachers/lists");
    }else{
      $this->error("修改失败");
    }
  }
  //修改状态
  public function setStatus(Request $request)
  {
    $data = $request->post();
    if (Teacher::update($data)) {
      $this->success("成功");
    } else {
      $this->error("失败");
    }
  }
  //修改是否可以点名
  public function setAttendances(Request $request)
  {
    $data = $request->post();
    if (Teacher::update($data)) {
      $this->success("成功");
    } else {
      $this->error("失败");
    }
  }
  //删除
  public function deletes(Request $request){
     $id = $request->post("id");
     $res = Teacher::get($id);
     //判断是否有有通过该老师报名的学生，如果有则不建议删除
     $count = Apply::where(["teacher_id"=>$id])->count("*");
     if(!$count) {
       delete_file($this->qr_src_path . $res['qr_src']); //删除数据的时候连同生成的图片一起删除
       if (Teacher::destroy($id)) {
//         Db::table("teacher")->execute("alter table teacher auto_increment = 1"); //设置主键为0
         $this->success("删除成功", "Teachers/lists");
       } else {
         $this->error("删除失败");
       }
     }else{
       $this->error("这位老师有报名学生，还不能删除。");
     }
  }
  //上传文件
  public function upload(Request $request){
    //接收上传的文件
    $file = $this->request->file('file');
    if(!empty($file)){
      //图片存的路径
      $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'teacher_qr';

      // 移动到框架应用根目录/public/uploads/ 目录下

      $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
      $error = $file->getError();
      //验证文件后缀后大小
      if(!empty($error)){
        dump($error);exit;
      }
      if($info){
        // 成功上传后 获取上传信息

        //获取图片的名字
        $imgName = $info->getFilename();
        //获取图片的路径
        $photo= $imgName;

      }else{
        // 上传失败获取错误信息
        $file->getError();
      }
    }else{
      $photo = '';
    }
    if($photo !== ''){
      return ['code'=>1,'msg'=>'成功','photo'=>$photo];
    }else{
      return ['code'=>404,'msg'=>'失败'];
    }
  }

  //上传文件
  public function upload_photo(Request $request){
    //接收上传的文件
    $file = $this->request->file('file');
    if(!empty($file)){
      //图片存的路径
      $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'teacher_photo';

      // 移动到框架应用根目录/public/uploads/ 目录下

      $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
      $error = $file->getError();
      //验证文件后缀后大小
      if(!empty($error)){
        dump($error);exit;
      }
      if($info){
        // 成功上传后 获取上传信息

        //获取图片的名字
        $imgName = $info->getFilename();
        //获取图片的路径
        $photo= $imgName;

      }else{
        // 上传失败获取错误信息
        $file->getError();
      }
    }else{
      $photo = '';
    }
    if($photo !== ''){
      return ['code'=>1,'msg'=>'成功','photo'=>$photo];
    }else{
      return ['code'=>404,'msg'=>'失败'];
    }
  }
}
